<template>
  <!--  @ok="handleSubmit"-->

  <BasicModal v-bind="$attrs" @register="register" showFooter :title="getTitle" width="50%">
    <div style="display: flex; padding-left: 30px">
      <div style="width: 50%">
        <div
          ><span>供应商：</span> <span>{{ searchInfo.supplierName }}</span></div
        >
        <div
          ><span>{{ searchInfo.num_title }}：</span> <span>{{ searchInfo.orderNumMax }}</span></div
        >
      </div>
      <div style="width: 50%">
        <div
          ><span>物料编号：</span><span>{{ searchInfo.materialNoInsStr }}</span></div
        >
        <div
          ><span>下单起始日期：</span><span>{{ searchInfo.orderDateStart }}</span></div
        >
      </div>
    </div>
    <BasicTable @register="registerTable">
      <template #form-custom> </template>
    </BasicTable>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicTree, TreeItem } from '@/components/Tree';
  import {
    purchaseOrderTableListApi,
    outsourceOrderTableListApi,
    getMouldItemsApi,
  } from '@/api/sys/msUser';
  import { BasicTable, useTable, TableAction, FormSchema } from '@/components/Table';
  import util from '@/utils/util';
  import { useMessage } from '@/hooks/web/useMessage';

  const emit = defineEmits(['success', 'register']);
  const isUpdate = ref(true);
  const treeData = ref<TreeItem[]>([]);
  const { createMessage } = useMessage();

  const clazzId = ref('');

  import { columns } from '../purchaseOrderList/role.data';
  import { out_columns } from '../outsourceOrderList/role.data';

  const searchFormSchema: FormSchema[] = [
    {
      field: 'materialName',
      label: '物料名称',
      component: 'Input',
      colProps: { span: 8 },
    },
  ];

  const getTitle = ref('');

  const searchInfo = ref({});

  var tableCfg = {
    title: '',
    api: purchaseOrderTableListApi,
    immediate: false, //是否立即请求接口
    columns: columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
  };

  const [registerTable, { reload, setProps }] = useTable(tableCfg);

  const [register, { setModalProps, closeModal }] = useModalInner(async (data) => {
    console.log('--获取数据:', data);
    getMouldItemsApi(data).then((res) => {
      if (res.code == 200) {
        let list = res.data;
        let idArrs = [];
        list.forEach((row) => {
          idArrs.push(row.mtid);
        });
        let title = '采购单明细';
        if (data.isReturn == 2) {
          title = '外协加工单明细';
          let c_arr = util.copy(columns);
          c_arr[0] = '外协加工单号';
          tableCfg.columns = c_arr;
          tableCfg.api = outsourceOrderTableListApi;
        }
        getTitle.value = title;
        tableCfg.title = title;
        let orderNumMax = '';
        let num_title = '';
        orderNumMax = data.returnConditionNum;

        //退款条件类别: 1一次性 2累计
        if (data.returnConditionType == 1) {
          num_title = '最大下单量';
        } else if (data.returnConditionType == 2) {
          num_title = '累计下单量';
        }
        tableCfg.searchInfo = {
          supplierId: data.supplierId,
          supplierName: data.supplierName,
          orderCountType: data.returnConditionType,
          orderNumMax,
          orderDateStart: data.startDate,
          materialNoInsStr: idArrs.join(','),
        };
        if (idArrs.length == 0) {
          tableCfg.searchInfo.materialNoInsStr = '未绑定该物料编号';
        }
        searchInfo.value = tableCfg.searchInfo;
        searchInfo.value.num_title = num_title;
        // console.log("--searchInfo::",tableCfg.searchInfo)
        setProps(tableCfg);

        reload();
      }
    });
  });
</script>
